package com.cloud.wms.ui.in.fragment;

import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.widget.expand.ExpandLayout;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseViewPagerFragment;
import com.cloud.wms.ui.in.contract.InShelfContract;
import com.cloud.wms.ui.in.entity.InMarkCommodityEntity;
import com.cloud.wms.ui.in.entity.InOrderEntity;
import com.cloud.wms.ui.in.presnter.InShelfPresenter;
import com.cloud.wms.ui.main.entity.TaskEntity;
import com.cloud.wms.ui.main.entity.TitleEntity;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.trello.rxlifecycle3.LifecycleTransformer;

import java.util.ArrayList;

import butterknife.BindView;

/**
 * @Author:Chen
 * @Date: 2021-01-24 19:35
 * @Description:
 */
public class ShelfMarkDetailFragment extends BaseViewPagerFragment<InShelfPresenter> implements InShelfContract.View {
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.refresh)
    SmartRefreshLayout refresh;
    private BaseQuickAdapter<TitleEntity<InMarkCommodityEntity.CommodityBean>, BaseViewHolder> mAdapter;
    private InOrderEntity inOrder;
    private TaskEntity taskEntity;
    private boolean isComplete;

    @Override
    protected int getResId() {
        return R.layout.fragment_in_inventory_have_detail;
    }

    @Override
    protected void setupComponent() {
        getFragmentComponent().inject(this);
    }

    @Override
    protected void initEvent(Bundle bundle) {
        inOrder = getArguments().getParcelable("InOrder");
        taskEntity = getArguments().getParcelable("Task");
        isComplete = getArguments().getBoolean("IsComplete");

        rvList.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new BaseQuickAdapter<TitleEntity<InMarkCommodityEntity.CommodityBean>, BaseViewHolder>(R.layout.item_in_shelf_mark_commodity) {
            @Override
            protected void convert(BaseViewHolder helper, TitleEntity<InMarkCommodityEntity.CommodityBean> item) {
                helper.setText(R.id.tv_mark_code, item.getTitle());
                ImageView ivArrow = helper.getView(R.id.iv_arrow);
                ExpandLayout expand = helper.getView(R.id.expand_layout);
                expand.setOnExpansionUpdateListener((expansionFraction, state) -> ivArrow.setRotation(expansionFraction * 180));
                helper.getView(R.id.ll_title_content).setOnClickListener(v -> expand.toggle(true));

                RecyclerView rvChildList = helper.getView(R.id.rv_child_list);
                rvChildList.setLayoutManager(new LinearLayoutManager(getContext()));
                BaseQuickAdapter mChildAdapter = new BaseQuickAdapter<InMarkCommodityEntity.CommodityBean, BaseViewHolder>(isComplete ? R.layout.item_in_shelf_mark_complete_commodity : R.layout.item_in_shelf_mark_wait_commodity, item.getChildList()) {
                    @Override
                    protected void convert(@NonNull BaseViewHolder helper, InMarkCommodityEntity.CommodityBean item) {
                        if (isComplete) {
                            helper.setText(R.id.tv_commodity_name, item.getCommodityName())
                                    .setText(R.id.tv_commodity_amount, item.getAmount() + "")
                                    .setText(R.id.tv_produce_date, item.getProductionDate())
                                    .setText(R.id.tv_deadline_date, item.getExpirationDate())
                                    .setText(R.id.tv_in_date, item.getInDate())
                                    .setText(R.id.tv_user_name, item.getUserName())
                                    .setText(R.id.tv_pos_code, item.getPosCode())
                                    .setText(R.id.tv_shelf_date, item.getShelfTime())
                                    .setText(R.id.tv_head, item.getNameAbbreviation());
                        } else {
                            helper.setText(R.id.tv_commodity_name, item.getCommodityName())
                                    .setText(R.id.tv_commodity_amount, item.getAmount() + "")
                                    .setText(R.id.tv_produce_date, item.getProductionDate())
                                    .setText(R.id.tv_deadline_date, item.getExpirationDate())
                                    .setText(R.id.tv_in_date, item.getInDate());
                        }
                    }
                };
                rvChildList.setAdapter(mChildAdapter);
            }
        };
        rvList.setItemAnimator(new DefaultItemAnimator());
        rvList.setAdapter(mAdapter);
        mPresenter.loadInMarkCommodityList(inOrder != null ? String.valueOf(inOrder.getInID()) :  taskEntity.getMainBillID() + "", isComplete);

        refresh.setOnRefreshListener(refreshLayout -> mPresenter.loadInMarkCommodityList(inOrder != null ? String.valueOf(inOrder.getInID()) :  taskEntity.getMainBillID() + "", isComplete));
    }

    @Override
    protected void initData() {

    }

    @Override
    public void showEmpty() {
        mAdapter.setEmptyView(getEmptyDataView());
        refresh.finishRefresh();
    }

    @Override
    public void showError() {
        mAdapter.setEmptyView(getErrorView());
        refresh.finishRefresh();
    }

    private View getEmptyDataView() {
        View notDataView = getLayoutInflater().inflate(com.cloud.core.R.layout.layout_empty, rvList, false);
        notDataView.setOnClickListener(v -> mPresenter.loadInMarkCommodityList(inOrder != null ? String.valueOf(inOrder.getInID()) :  taskEntity.getMainBillID() + "", false));
        return notDataView;
    }

    private View getErrorView() {
        View errorView = getLayoutInflater().inflate(com.cloud.core.R.layout.layout_error, rvList, false);
        errorView.setOnClickListener(v -> mPresenter.loadInMarkCommodityList(inOrder != null ? String.valueOf(inOrder.getInID()) :  taskEntity.getMainBillID() + "", false));
        return errorView;
    }

    @Override
    public void onSuccess(Message message) {
        switch (message.what) {
            case InShelfPresenter.IN_ORDER_MARK_DETAIL:
                refresh.finishRefresh();
                ArrayList<TitleEntity<InMarkCommodityEntity.CommodityBean>> list = (ArrayList<TitleEntity<InMarkCommodityEntity.CommodityBean>>) message.obj;
                mAdapter.setNewInstance(list);
                break;
        }
    }

    @Override
    public void info(String msg) {
        super.info(msg);
        refresh.finishRefresh();
    }

    @Override
    public void error(String msg) {
        super.error(msg);
        refresh.finishRefresh();
    }

    @Override
    public <R> LifecycleTransformer<R> toLifecycle() {
        return bindToLifecycle();
    }

    public static BaseViewPagerFragment newInstance(Bundle bundle) {
        ShelfMarkDetailFragment fragment = new ShelfMarkDetailFragment();
        fragment.setArguments(bundle);
        return fragment;
    }
}
